// packagePay/pages/projectbill/projectbill.js
import {
    stopPay,
    getbillinfoapi,
    getprojectbillinfoapi,
    exportWorkerPriceList,
    stopAllPay
} from '../../api/pay'
const app = getApp()
import user from '../../../utils/user.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        fixedflag: false,
        i: '',
        pageSize: 10,
        pageNum: 1,
        hasnextpage: true,
        id: null,
        month: "",
        billinfomsg: {},
        showallflag: false,
        data2: [],
        listtabarr: [{
            name: '全部',
            index: 0,
            active: true,
            value: ''
        }, {
            name: '已领薪',
            index: 1,
            active: false,
            value: 1
        }, {
            name: "待领薪",
            index: 2,
            active: false,
            value: 0
        }, {
            name: "停止发薪",
            index: 3,
            active: false,
            value: 2
        }, {
            name: "领薪失败",
            index: 4,
            active: false,
            value: 4
        }, {
            name: "进行中",
            index: 5,
            active: false,
            value: 3
        }],
        isSeatAdmin: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options, 'optionsoptionsoptionsoptions');
        user.waitLogin().then(() => {
            this.setData({
                isSeatAdmin: user.isSeatAdmin
            })
        })
        this.setData({
            month: options.month,
            id: options.id
        }, () => {
            this.getbillinfo(options.id)
            this.getprojectbillinfo()
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    onPageScroll(e) {
        let query = wx.createSelectorQuery()
        query.select('.listbox').boundingClientRect((rect) => {
            let top = rect.top
            console.log(top)
            if (top <= 110) {
                this.setData({
                    fixedflag: true
                })
            } else {
                this.setData({
                    fixedflag: false
                })
            }
        }).exec()
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    onStopAll() {
        app.confirm('是否确认停止该订单内所有员工发薪，已提现或进行中的员工不会影响').then(() => {
            console.log('此接口有时候会传递undefined给后端')
            stopAllPay({
                processId: this.data.id ? this.data.id : null
            }).then(res => {
                app.alert(res).then(() => {
                    setTimeout(() => {
                        this.setData({
                            data2: [],
                            pageNum: 1,
                            hasnextpage: true
                        }, () => {
                            this.getbillinfo(this.data.id)
                            this.getprojectbillinfo()
                        })
                    }, 1000)
                })
            })
        })

    },
    exportWorkerPriceList() {
        exportWorkerPriceList({
            processId: this.data.id
        }).then(res => {
            const url = `${app.globalData.baseUrl}/common/download?fileName=${encodeURIComponent(res)}&delete=false`
            if (wx.canIUse('openDocument')) {
                const file = wx.env.USER_DATA_PATH + '/' + this.data.billinfomsg.projectName.replace(/\//g, '.') + '.xlsx'
                wx.downloadFile({
                    url,
                    filePath: file,
                    success: function () {
                        const documentPath = file
                        wx.openDocument({
                            showMenu: true,
                            filePath: documentPath,
                            fileType: 'xlsx'
                        })
                    }
                })
            } else {
                this.setData({
                    copyLinkStatus: true,
                    copyLinkContent: url
                })
            }
        })
    },
    onStop(event) {
        app.confirm('是否确认停止该员工发薪').then(() => {
            stopPay({
                id: event.currentTarget.dataset.id
            }).then(() => {
                app.toast('停止发薪成功', 'success')
                setTimeout(() => {
                    this.setData({
                        pageNum: 1,
                        data2: []
                    }, () => {
                        this.getbillinfo(this.data.id)
                        this.getprojectbillinfo()
                    })
                }, 1000)

                })
        })
    },
    getbillinfo(id) {
        getbillinfoapi({
            id: id
        }).then((res) => {
            // console.log(res, '111111');
            this.setData({
                billinfomsg: res
            })
            console.log(this.data.billinfomsg);
        })
    },
    getprojectbillinfo() {
        this.data.listtabarr.forEach((ele, i) => {
            if (ele.active) {
                this.data.i = ele.value
            }
        })
        getprojectbillinfoapi({
            // queryMonth: this.data.month,
            processId: this.data.id,
            status: this.data.i,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize,
        }).then((res) => {
            console.log(res, '22222');
            res.list.forEach(ele => {
                ele.idCardNo4 = ele.idCardNo.substr(-4)
            })
            let arr = []
            arr.push(...this.data.data2)
            arr.push(...res.list)
            this.setData({
                data2: arr,
                hasnextpage: res.hasNextPage
            })
        })
    },
    showall() {
        this.setData({
            showallflag: true,
        })
    },
    call(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.num
        })
    },
    closeshowall() {
        this.setData({
            showallflag: false,
        })
    },
    changelisttab(e) {
        console.log(e.currentTarget.dataset.index);
        this.data.listtabarr.forEach((ele, index) => {
            ele.active = false
        })
        let i = e.currentTarget.dataset.index
        this.data.listtabarr[i].active = !this.data.listtabarr[i].active
        console.log(this.data.listtabarr);
        this.setData({
            data2: [],
            listtabarr: this.data.listtabarr,
            pageNum: 1,
        }, () => {
            this.getprojectbillinfo()
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log(111);
        if (this.data.hasnextpage) {
            this.setData({
                pageNum: this.data.pageNum + 1
            }, () => {
                this.getprojectbillinfo()
            })

        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})